package session.stateless;

import entity.AccessRightsEntity;
import entity.StaffAccountEntity;
import entity.StaffEntity;
import entity.UserAccountEntity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import util.enumeration.AccessRight;
import util.enumeration.Department;
import util.enumeration.Gender;
import util.enumeration.JobPosition;

/**
 *
 * @author WenYong
 */
@Stateless
@LocalBean
public class TCubeSessionBean {

    @PersistenceContext
    private EntityManager entityManager;

    public boolean addTCubeUser(Department department, JobPosition job,
            String firstName, String lastName,
            Date bithDate, String email, String password,
            String confirmPassword, Gender gender,
            Date joinDate, List<AccessRight> accessRights) {
        boolean result = false;
        try {
            StaffEntity staff = new StaffEntity();
            staff.setBirthdate(bithDate);
            staff.setEmail(email);
            staff.setEmailSecondary(email);
            staff.setGender(gender);
            staff.setLastName(lastName);
            staff.setDepartment(department);
            staff.setJobPosition(job);
            staff.setJoinDate(joinDate);

            StaffAccountEntity staffAccount = new StaffAccountEntity();
            Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
            String dateNow = formatter.format(currentDate.getTime());
            Date cDate = (Date) formatter.parse(dateNow);
            staffAccount.setCreationDate(cDate);
            staffAccount.setPassword(password);
            staffAccount.setStatus(true);
            staffAccount.setUsername(email);
            staffAccount.setStaff(staff);
            List<AccessRightsEntity> temp = new ArrayList();
            for (int i = 0; i < accessRights.size(); i++) {
                AccessRightsEntity a = new AccessRightsEntity();
                a.setAccessRight(accessRights.get(i));
//                a.setTcubeStaff(staffAccount);
                temp.add(a);
            }
            staffAccount.setAccessRights(temp);
            entityManager.persist(staffAccount);
            entityManager.flush();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public StaffAccountEntity getUser(String username) {
        Query query = entityManager.createQuery("SELECT a FROM Account a WHERE a.username = :inUsername ");
        query.setParameter("inUsername", username);
        StaffAccountEntity t = null;
        try {
            if (query.getSingleResult().getClass().getName().equalsIgnoreCase("entity.TCubeStaffAccount")) {
                t = (StaffAccountEntity) query.getSingleResult();
            }
        } catch (NoResultException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.getMessage();
        }
        return t;
    }

    public void updateStatus(StaffAccountEntity user) {
        UserAccountEntity u = entityManager.find(StaffAccountEntity.class, user.getId());
        if (u != null) {
            u.setStatus(user.getStatus());
            entityManager.persist(u);
            entityManager.flush();
        }
    }

    public List<StaffAccountEntity> getTCubeAccountList() {
        List<StaffAccountEntity> TCubeList = new ArrayList();
        Query query = entityManager.createQuery("SELECT a FROM StaffAccount a");
        List<StaffAccountEntity> accountList = query.getResultList();
        StaffAccountEntity t = null;
        for (int i = 0; i < accountList.size(); i++) {            
            if (accountList.get(i).getClass().getName().equalsIgnoreCase("entity.TCubeStaffAccountEntity")) {
                t = (StaffAccountEntity) accountList.get(i);
                TCubeList.add(t);
            }
        }
        return TCubeList;
    }
}
